﻿/*******************************************************************************
 * AAEngine
 * Copyright (c) 2010 Mike Jarosch
 *
 * This file is licensed under the terms of the MIT license, which is included
 * in the "MIT License.txt" file at the root directory of this SDK.
 ******************************************************************************/

using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace AAEngine.Screens
{
    public class SplashScreen : GameScreen
    {
        Texture2D _image;
        float _duration;
        TimeSpan _displayTime;
        string _imageName;
        GameScreen[] _nextScreens;
        InputState _input;

        public SplashScreen(string imageName, float displayTime, float fadeInTime, float fadeOutTime, params GameScreen[] nextScreens)
        {
            _imageName = imageName;
            _nextScreens = nextScreens;
            _displayTime = TimeSpan.FromSeconds(displayTime);
            TransitionOnTime = TimeSpan.FromSeconds(fadeInTime);
            TransitionOffTime = TimeSpan.FromSeconds(fadeOutTime);
            _input = new InputState();
        }

        public override void HandleInput(InputState input)
        {
            if (ScreenState == ScreenState.Active)
            {
                if (input.IsAnyKeyDown())
                {
                    ExitScreen();
                    return;
                }
            }
        }

        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);

            if (ScreenState == ScreenState.Active)
            {
                float delta;
                if (_displayTime == TimeSpan.Zero)
                {
                    delta = 1;
                }
                else
                {
                    delta = (float)(gameTime.ElapsedGameTime.TotalMilliseconds / _displayTime.TotalMilliseconds);
                }

                _duration += delta;

                if (_duration >= 1)
                {
                    ExitScreen();
                }
            }
        }

        public override void LoadContent()
        {
            base.LoadContent();

            _image = Content.Load<Texture2D>(_imageName);
        }

        public override void Draw(Microsoft.Xna.Framework.GameTime gameTime)
        {
            base.Draw(gameTime);

            Viewport viewport = ScreenManager.Game.GraphicsDevice.Viewport;
            Rectangle destinationRectangle = new Rectangle(0, 0, viewport.Width, viewport.Height);
            ScreenManager.SpriteBatch.Begin();
            ScreenManager.SpriteBatch.Draw(_image, destinationRectangle, Color.White);
            ScreenManager.SpriteBatch.End();

            ScreenManager.FadeBackBufferToBlack(TransitionPosition);
        }

        public override void OnRemoved() 
        {
            foreach (GameScreen screen in _nextScreens)
            {
                ScreenManager.AddScreen(screen);
            }
        }
    }
}
